package com.alog.storage.adapter;

import android.content.Context;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import com.alog.storage.domain.ViewHolder;
import com.alog.storage.ui.widget.PinnedHeadExpandableListView;
import java.util.ArrayList;
import java.util.List;

/**
 * 可展开adapter基类, 供各adapter继承
 */
public abstract class BaseExpandableAdapter<T> extends BaseExpandableListAdapter implements PinnedHeadExpandableListView.HeaderAdapter {
    protected Context mContext;
    protected int mGroupLayoutId, mChildLayoutId;
    public List<T> dataList = new ArrayList<>();
    private PinnedHeadExpandableListView mListView;
    private SparseIntArray groupStatusMap = new SparseIntArray();

    public BaseExpandableAdapter(Context context, int groupLayoutId, int childLayoutId, List<T> dataList, PinnedHeadExpandableListView listView) {
        this.mContext = context;
        this.mGroupLayoutId = groupLayoutId;
        this.mChildLayoutId = childLayoutId;
        this.dataList = dataList;
        this.mListView = listView;
    }

    @Override
    public int getGroupCount() {
        if (dataList != null) {
            return dataList.size();
        }
        return 0;
    }

    @Override
    public T getGroup(int groupPosition) {
        if (dataList != null && dataList.size() > groupPosition) {
            return dataList.get(groupPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, mGroupLayoutId, groupPosition);
        setDataToGroupView(viewHolder, getGroup(groupPosition));
        return viewHolder.getConvertView();
    }

    /**
     * 子类实现此方法设置数据到GroupView上面
     *
     * @param viewHolder
     * @param groupItem
     */
    public abstract void setDataToGroupView(ViewHolder viewHolder, T groupItem);

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = ViewHolder.get(mContext, convertView, parent, mChildLayoutId, groupPosition, childPosition);
        setDataToChildView(viewHolder, getChild(groupPosition, childPosition));
        return viewHolder.getConvertView();
    }

    /**
     * 子类实现此方法设置数据到ChildView上面
     *
     * @param viewHolder
     * @param childItem
     */
    public abstract void setDataToChildView(ViewHolder viewHolder, Object childItem);

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public int getHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if (childPosition == childCount - 1) {
            return PINNED_HEADER_PUSHED_UP;
        } else if (childPosition == -1 && !mListView.isGroupExpanded(groupPosition)) {
            return PINNED_HEADER_GONE;
        } else {
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if (groupPosition >= 0 && groupStatusMap.size() > groupPosition && groupStatusMap.keyAt(groupPosition) >= 0) {
            return groupStatusMap.get(groupPosition);
        } else {
            return 0;
        }
    }

    public void onDestroy() {
        mContext = null;
        mGroupLayoutId = 0;
        mChildLayoutId = 0;
        if (dataList != null) {
            dataList.clear();
            dataList = null;
        }
    }

}
